package com.auoem.radio.common.custview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auctus.am.framework.utils.LogM;
import com.auoem.radio.R;
import com.auoem.radio.common.interfaces.AuCallBack;
import com.auoem.radio.common.utils.ResourceUtils;

public class CustomTitlebar extends RelativeLayout implements
        View.OnClickListener {

    public final static int IMG_DEFAULT = 0;
    public final static int IMG_NO = -1;

    private ImageView mIvLeft, mIvRight, mIvAlRight;
    private TextView mTvTitle, mTvBack, mTvLeft, mTvRight;
    private RelativeLayout mRlView;

    private AuCallBack mReturnCb;
    private ButtonCallback mButtonCallback;

    private int titleText, rightText;

    private String strTitleText, strLeftText, strRightText;
    private int imgLeft = IMG_DEFAULT, imgRight = IMG_NO, imgSmtRight = IMG_NO;

    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_titlebar_back:
                case R.id.iv_titlebar_left:
                    if (null != mButtonCallback)
                        mButtonCallback.OnClickResultCallback(
                                ButtonType.LeftimgBtn, v);
                    if (mReturnCb != null) {
                        mReturnCb.onCb(1);
                    }
                    break;
                case R.id.tv_titlebar_right:
                    if (null != mButtonCallback)
                        mButtonCallback.OnClickResultCallback(
                                ButtonType.RighttvBtn, v);
                    break;
                case R.id.iv_titlebar_right:
                    if (null != mButtonCallback)
                        mButtonCallback.OnClickResultCallback(
                                ButtonType.RightimgBtn, v);
                    break;
                case R.id.iv_titlebar_alright:
                    if (null != mButtonCallback)
                        mButtonCallback.OnClickResultCallback(
                                ButtonType.AlRightimgBtn, v);
                    break;
            }
        }
    };

//    public CustomTitlebar(Context context) {
//        super(context);
//        inflateView(context);
//    }

    public CustomTitlebar(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateView(context);
        attrs(context, attrs, 0);
    }

    public CustomTitlebar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflateView(context);
        attrs(context, attrs, defStyle);
    }

    private void attrs(Context context, AttributeSet attrs, int defStyle) {
//        ViewGroup.LayoutParams layoutParams = getLayoutParams();
//        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        setLayoutParams(layoutParams);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitlebar);
//        titleText = typedArray.getInt(R.styleable.CustomTitlebar_titleText, 0);
//        rightText = typedArray.getInt(R.styleable.CustomTitlebar_rightText, 0);
//        if (rightText != 0) {
//            mTvRight.setText(rightText);
//        }
//        if (titleText != 0) {
//            mTvTitle.setText(titleText);
//        }
        strTitleText = typedArray.getString(R.styleable.CustomTitlebar_titleText);
        strRightText = typedArray.getString(R.styleable.CustomTitlebar_rightText);
        strLeftText = typedArray.getString(R.styleable.CustomTitlebar_leftText);

        imgLeft = typedArray.getResourceId(R.styleable.CustomTitlebar_leftImg, IMG_DEFAULT);
        imgRight = typedArray.getResourceId(R.styleable.CustomTitlebar_rightImg, IMG_NO);
        imgSmtRight = typedArray.getInt(R.styleable.CustomTitlebar_rightSmtImg, IMG_NO);

        LogM.d("CstTitle", "attr: title: " + strTitleText + " right: " + strRightText + " leftImgId: " + imgLeft);

        if (!TextUtils.isEmpty(strLeftText)) {
            mTvLeft.setText(strLeftText);
        }
        if (!TextUtils.isEmpty(strRightText)) {
            mTvRight.setText(strRightText);
        }
        if (!TextUtils.isEmpty(strTitleText)) {
            mTvTitle.setText(strTitleText);
        }
        updateImg(imgLeft, imgRight, imgSmtRight);
        typedArray.recycle();
    }

    public void updateImg(int imgLeft, int imgRight, int imgSmtRight) {
        updateImgLeft(imgLeft);
        updateImgRight(imgRight);
        updateImgRightSmt(imgSmtRight);
    }

    public void updateImgLeft(int imgLeft) {
        this.imgLeft = imgLeft;
        mIvLeft.setVisibility(imgLeft == IMG_NO ? GONE : VISIBLE);
        if (imgLeft == IMG_NO) {
            return;
        }
        if (imgLeft == IMG_DEFAULT) {
            mIvLeft.setImageResource(R.mipmap.hl_common_back);
            return;
        }
        mIvLeft.setImageResource(imgLeft);
    }

    public void updateImgRight(int imgRight) {
        this.imgRight = imgRight;
        mIvRight.setVisibility(imgRight == IMG_NO ? GONE : VISIBLE);
        if (imgRight == IMG_NO) {
            return;
        }
        if (imgRight == IMG_DEFAULT) {
            mIvRight.setImageResource(R.drawable.btn_public_titlebar_more_normal);
            return;
        }
        mIvRight.setImageResource(imgRight);
    }

    public void updateImgRightSmt(int imgRightSmt) {
        this.imgSmtRight = imgRightSmt;
        mIvAlRight.setVisibility(imgRightSmt == IMG_NO ? GONE : VISIBLE);
        if (imgRightSmt == IMG_NO) {
            return;
        }
        if (imgRightSmt == IMG_DEFAULT) {
            mIvAlRight.setImageResource(R.drawable.btn_public_titlebar_more_normal);
            return;
        }
        mIvAlRight.setImageResource(imgRightSmt);
    }

    public void setReturnCb(AuCallBack cb) {
        mReturnCb = cb;
    }

    private void inflateView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_custom_titlebar, this);
        mRlView = (RelativeLayout) findViewById(R.id.rl_titlebar_view);
        mIvLeft = (ImageView) findViewById(R.id.iv_titlebar_left);
        mTvTitle = (TextView) findViewById(R.id.tv_titlebar_title);
        mTvBack = (TextView) findViewById(R.id.tv_titlebar_back);

        mTvRight = (TextView) findViewById(R.id.tv_titlebar_right);
        mTvLeft = findViewById(R.id.tv_titlebar_back);
        mIvRight = (ImageView) findViewById(R.id.iv_titlebar_right);
        mIvAlRight = (ImageView) findViewById(R.id.iv_titlebar_alright);

        if (!this.isInEditMode()) {
            mIvLeft.setOnClickListener(mOnClickListener);
            mTvBack.setOnClickListener(mOnClickListener);
            mTvRight.setOnClickListener(mOnClickListener);
            mTvLeft.setOnClickListener(mOnClickListener);
            mIvRight.setOnClickListener(mOnClickListener);
            mIvAlRight.setOnClickListener(mOnClickListener);
        }
    }

    /**
     * 自定义标题栏
     *
     * @param left     左边文字，null或空格不显示
     * @param imgLeft
     * @param title    标题文字，null或空格不显示
     * @param right    右边文字，null或空格不显示
     * @param imgRight
     */
    public void initCustom(String left, int imgLeft, String title,
                           String right, int imgRight) {
        if (TextUtils.isEmpty(left)) {
            mTvBack.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(left)) {
            mTvBack.setVisibility(View.VISIBLE);
            mTvBack.setText(left);
        }

        if (imgLeft == IMG_NO) {
            mIvLeft.setVisibility(View.GONE);
        } else if (imgLeft == IMG_DEFAULT) {
            mIvLeft.setVisibility(View.VISIBLE);
        } else {
            mIvLeft.setImageResource(imgLeft);
        }

        if (TextUtils.isEmpty(title)) {
            mTvTitle.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(title)) {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(title);
        }

        if (TextUtils.isEmpty(right)) {
            mTvRight.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(right)) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(right);
        }

        if (TextUtils.isEmpty(right)) {
            mTvLeft.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(right)) {
            mTvLeft.setVisibility(View.VISIBLE);
            mTvLeft.setText(right);
        }

        if (imgRight == IMG_NO) {
            mIvRight.setVisibility(View.GONE);
        } else if (imgRight == IMG_DEFAULT) {
            mIvRight.setVisibility(View.VISIBLE);
        } else {
            mIvRight.setImageResource(imgRight);
        }

    }

    /**
     * 没有返回 只有标题
     *
     * @param title
     */
    public void newInitOnlyTitle(String title) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
    }

    /**
     * 标题和返回按键
     *
     * @param title
     */
    public void newInitTitleAndBack(String title) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
    }

    /**
     * 返回按键 标题 右边文字
     *
     * @param title
     * @param right
     */
    public void newInitTitleAndRightTv(String title, String right) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.VISIBLE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
        if (null != right)
            setRightTxt(right);
    }

    /**
     * 标题 右边文字
     *
     * @param title
     * @param right
     */
    public void newInitTitleRightTvNoBack(String title, String right) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.VISIBLE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
        if (null != right)
            setRightTxt(right);
    }

    /**
     * 标题 右边图标
     *
     * @param title
     * @param resourId
     */
    public void newInitTitleRightImgNoBack(String title, int resourId) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.VISIBLE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
        if (-1 != resourId)
            setRightIV2Background(resourId);
    }

    /**
     * 返回按键 标题 右边图标
     *
     * @param title
     * @param resourId
     */
    public void newInitTitleAndRightImg(String title, int resourId) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.VISIBLE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
        if (-1 != resourId)
            setRightIV2Background(resourId);
    }

    /**
     * 只有标题 ，没有返回
     *
     * @param title newInitOnlyTitle(String title) instead
     */
    @Deprecated
    public void initViewOnlyTitle(String title) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
    }

    /**
     * 返回和标题
     *
     * @param title newInitTitleAndBlack(String title) instead
     */
    @Deprecated
    public void initViewNoRight(String back, String title) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvBack.setVisibility(View.GONE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title) {
            mTvTitle.setVisibility(View.VISIBLE);
            setTitleTxt(title);
        }
    }

    /**
     * 只有返回，没有标题
     */
    @Deprecated
    public void initViewNoTitle() {
        initViewNoTitle(null);
    }

    /**
     * 只有标题
     *
     * @param title {newInitTitleAndBlack instead }
     */
    @Deprecated
    public void initViewNoTitle(String title) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvBack.setVisibility(View.GONE);
        mTvRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title) {
            mTvTitle.setVisibility(View.VISIBLE);
            setTitleTxt(title);
        }
    }

    @Deprecated
    public void initLeftAndRightTxt(String back, String right) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.VISIBLE);
        mTvTitle.setVisibility(View.GONE);
        mTvRight.setVisibility(View.VISIBLE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != back)
            setBackTxt(back);
        if (null != right)
            setRightTxt(right);
    }

    /**
     * 有返回 有右边文字 有标题
     *
     * @param back
     * @param right
     */
    @Deprecated
    public void initViewNoTitle(String back, String right) {
        /*
         * mIvLeft.setVisibility(View.VISIBLE);
         * mTvBack.setVisibility(View.VISIBLE);
         * mTvTitle.setVisibility(View.GONE);
         * mTvRight.setVisibility(View.VISIBLE);
         * mIvRight.setVisibility(View.GONE);
         * mIvAlRight.setVisibility(View.GONE); if (null != back)
         * setBackTxt(back); if (null != back) { setRightTxt(right); }
         */
        initViewNoRightImg(back, right);
    }

    /**
     * 没有右边图片,有标题
     *
     * @param title
     * @param right
     */
    @Deprecated
    public void initViewNoRightImg(String title, String right) {
        initViewNoRightImg(null, title, right);
    }

    /**
     * 没有右边图片，有标题、左边文字、左边返回键
     *
     * @param back
     * @param title
     * @param right
     */
    @Deprecated
    public void initViewNoRightImg(String back, String title, String right) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvRight.setVisibility(View.VISIBLE);
        mIvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.GONE);
        if (null != title)
            setTitleTxt(title);
        if (null != right)
            setRightTxt(right);
        if (null != back) {
            mTvBack.setVisibility(View.VISIBLE);
            setBackTxt(back);
        }
    }

    /**
     * 没有右边文字、左边返回
     *
     * @param title
     * @param imgRsource1
     * @param imgRsour2   newInitTitleAndRightImg(String title,int imgRsour2) instead
     */
    @Deprecated
    public void initViewNoRightTxt(String title, int imgRsource1, int imgRsour2) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        if (null != title)
            setTitleTxt(title);
        mTvRight.setVisibility(View.GONE);
        if (-1 == imgRsource1) {
            mIvAlRight.setVisibility(View.GONE);
        } else {
            mIvAlRight.setVisibility(View.VISIBLE);
            mIvAlRight.setImageResource(imgRsource1);
        }

        if (-1 == imgRsour2) {
            mIvRight.setVisibility(View.GONE);
        } else {
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(imgRsour2);
        }

    }

    /**
     * @param back
     * @param imgRsource1
     * @param imgRsour2   newInitTitleAndRightImg(String title,int imgRsour2) instead
     */
    @Deprecated
    public void initViewNoTitle(String back, int imgRsource1, int imgRsour2) {
        mIvLeft.setVisibility(View.VISIBLE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        if (null != back)
            setTitleTxt(back);
        mTvRight.setVisibility(View.GONE);
        if (-1 == imgRsource1) {
            mIvAlRight.setVisibility(View.GONE);
        } else {
            mIvAlRight.setVisibility(View.VISIBLE);
            mIvAlRight.setImageResource(imgRsource1);
        }

        if (-1 == imgRsour2) {
            mIvRight.setVisibility(View.GONE);
        } else {
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(imgRsour2);
        }
    }

    /**
     * 没有右边文字和右起第二张图片
     *
     * @param title
     * @param imgRsource1
     */
    @Deprecated
    public void initViewNoRightTxt(String title, int imgRsource1) {
        initViewNoRightTxt(title, imgRsource1, -1);
    }

    @Deprecated
    public void initViewNoLeft(String title, int rightFirstImg,
                               int rightSecondImg) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.GONE);
        mTvRight.setVisibility(View.GONE);
        mIvAlRight.setVisibility(View.VISIBLE);
        mIvAlRight.setImageResource(rightFirstImg);
        mIvRight.setVisibility(View.VISIBLE);
        mIvRight.setImageResource(rightSecondImg);
    }

    @Deprecated
    public void initViewWithTitleAndRight(String title, String right) {
        mIvLeft.setVisibility(View.GONE);
        mTvBack.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText(right);
        mIvAlRight.setVisibility(View.GONE);
        mIvRight.setVisibility(View.GONE);
    }

    public void setRightTxt(String text) {
        mTvRight.setText(text);
        setRightEnable(!TextUtils.isEmpty(text));
    }

    public void setTitleTxtColors(int titleColor) {
        mTvTitle.setTextColor(titleColor);
    }

    public void setTitleTxt(String title) {
        mTvTitle.setText(title);
    }

    public void setRightTxtColor(int color) {
        mTvRight.setTextColor(ResourceUtils.getResourcesColor(color));
    }

    public void setRightEnable(boolean enabled) {
        if (enabled) {
            mTvRight.setVisibility(View.VISIBLE);
        }
        mTvRight.setEnabled(enabled);
    }

    public void setRightTxtColorList(int color) {
        mTvRight.setTextColor(getResources().getColorStateList(color));
    }

    /**
     * @param back setTitleTxt instead
     */
    public void setBackTxt(String back) {
        setTitleTxt(back);
    }

    public void setBackImg(int res) {
        mIvLeft.setVisibility(View.VISIBLE);
        mIvLeft.setImageResource(res);
    }

    public void hideRightButton() {
        mIvRight.setVisibility(View.GONE);
    }

    public void setRightTxtVisibility(boolean visible) {
        mTvRight.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public enum ButtonType {
        LeftimgBtn, TitleBtn, RighttvBtn, RightimgBtn, AlRightimgBtn
    }

    public void setButtonOnClickCallback(ButtonCallback callback) {
        this.mButtonCallback = callback;

    }

    public interface ButtonCallback {
        void OnClickResultCallback(ButtonType btnType, View view);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    public void setRightIV2Background(int rightResouce) {
        mIvRight.setImageResource(rightResouce);
    }

    public void setRightIV2Enable(boolean enabled) {
        mIvRight.setEnabled(enabled);
    }

    public void setRightIV2Visible(boolean isvisible) {
        mIvRight.setVisibility(isvisible ? View.VISIBLE : View.GONE);
    }

    public void setBackground(int drawableId) {
        mRlView.setBackgroundResource(drawableId);
    }

    public void setBackgroundColor(int colorId) {
        mRlView.setBackgroundColor(colorId);
    }
}
